app.controller('ContactCtrl', ['$scope', '$http', '$filter', function ($scope, $http, $filter) {
    $http.get('/contact/all').then(function (resp) {
        $scope.items = resp.data.data;
        $scope.item = $filter('orderBy')($scope.items, 'first')[0];
        $scope.item.selected = true;
    });
    $http.get("/contact/group").then(function (resp) {
        $scope.groups=resp.data.data;
    })

    $scope.filter = '';
    // $scope.groups = [
    //     {name: 'Coworkers'},
    //     {name: 'Family'},
    //     {name: 'Friends'},
    //     {name: 'Partners'},
    //     {name: 'Group'}
    // ];

    $scope.createGroup = function () {
        var group = {name: 'New Group'};
        group.name = $scope.checkItem(group, $scope.groups, 'name');
        $scope.groups.push(group);
        $http.post("/contact/group",group).then(function (resp) {
            //toaster
        })
    };

    $scope.checkItem = function (obj, arr, key) {
        var i = 0;
        angular.forEach(arr, function (item) {
            if (item[key].indexOf(obj[key]) == 0) {
                var j = item[key].replace(obj[key], '').trim();
                if (j) {
                    i = Math.max(i, parseInt(j) + 1);
                } else {
                    i = 1;
                }
            }
        });
        return obj[key] + (i ? ' ' + i : '');
    };

    $scope.deleteGroup = function (item) {
        $scope.groups.splice($scope.groups.indexOf(item), 1);
        $http.delete("/contact/group/"+item.name).then(function (resp) {
            //toaster
        })
    };

    $scope.selectGroup = function (item) {
        angular.forEach($scope.groups, function (item) {
            item.selected = false;
        });
        $scope.group = item;
        $scope.group.selected = true;
        $scope.filter = item.name;
    };

    $scope.selectItem = function (item) {
        angular.forEach($scope.items, function (item) {
            item.selected = false;
            item.editing = false;
        });
        $scope.item = item;
        $scope.item.selected = true;
    };

    $scope.deleteItem = function (item) {
        $scope.items.splice($scope.items.indexOf(item), 1);
        $scope.item = $filter('orderBy')($scope.items, 'first')[0];
        if ($scope.item) {
            $scope.item.selected = true;
        }
        $http.delete('/contact/del/'+item.id).then(function (resp) {
            //toaster
        })
    };

    $scope.createItem = function () {
        var item = {
            group: 'Friends',
            avatar: '/static/img/a0.jpg'
        };
        $scope.items.push(item);
        $scope.selectItem(item);
        $scope.item.editing = true;
    };

    $scope.editItem = function (item) {
        if (item && item.selected) {
            item.editing = true;
        }
    };

    $scope.doneEditing = function (item) {
        item.editing = false;
        if($scope.group.name == ''){
            item.group = '';
        }else {
            item.group = $scope.group.name;
        }
        if(item.id>0){
            $http.post('/contact/update',item).then(function (resp) {
                //toaster
            })
        }else {
            $http.post('/contact/add',item).then(function (resp) {
                //toaster
            })
        }
    };
    
    $scope.doneEditGroup = function (item) {
        item.editing = false;
        $http.put("/contact/group",item).then(function (resp) {
            //toaster
        })
    }
}]);